package com.group6.exam.teacher.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.entity.PaperEntity;
import com.group6.exam.teacher.service.IPageService;
import com.group6.exam.teacher.service.pageServiceImpl;

public class PageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		String command = request.getParameter("command");
		if ("maintain".equals(command)) {
			maintain(request, response, out);
		} else if ("delete".equals(command)) {
			deletePaper(request, response, out);
		} else if ("initUpdate".equals(command)) {
			initUpdate(request, response, out);
		} else if ("update".equals(command)) {
			update(request, response, out);
		} else if ("initType".equals(command)) {
			initType(request, response, out);
		} else if ("addPaper".equals(command)) {
			addPaper(request, response, out);
		} else if ("paperBuild".equals(command)) {
			paperBuild(request, response, out);
		} else if ("autoBuild".equals(command)) {
			autoBuild(request, response, out);
		} else if ("paperScan".equals(command)) {
			paperScan(request, response, out);
		} else if ("scan".equals(command)) {
			scan(request, response, out);
		}
		out.flush();
		out.close();
	}

	/**
	 * 试卷预览
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @throws IOException
	 * @throws ServletException
	 */
	private void scan(HttpServletRequest request, HttpServletResponse response,
			PrintWriter out) throws ServletException, IOException {
		IPageService PageService = new pageServiceImpl();
		// 获取试卷列表信息
		String paperId = request.getParameter("paperId");
		Map<String, Object> map = PageService.getPaperById(paperId);

		request.setAttribute("paper", map);

		// 初始化试卷的试题
		List<Map<String, Object>> singleList = PageService
				.getQuestionsByTypeId("1",
						String.valueOf(map.get("single_number")));
		List<Map<String, Object>> multiList = PageService.getQuestionsByTypeId(
				"2", String.valueOf(map.get("multi_number")));
		List<Map<String, Object>> judgeList = PageService.getQuestionsByTypeId(
				"3", String.valueOf(map.get("judge_number")));
		if (judgeList != null && multiList != null && singleList != null) {
			request.setAttribute("singleList", singleList);
			request.setAttribute("multiList", multiList);
			request.setAttribute("judgeList", judgeList);

			request.getRequestDispatcher("/WEB-INF/view/exam/exam.jsp")
					.forward(request, response);
		} else {
			out.print("<script>alert('查看试卷失败')</script>");
		}

	}

	/**
	 * 初始化试卷列表
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @throws IOException
	 * @throws ServletException
	 */
	private void paperScan(HttpServletRequest request,
			HttpServletResponse response, PrintWriter out)
			throws ServletException, IOException {
		IPageService PageService = new pageServiceImpl();

		// 分页查询
		PageModel pageModel = new PageModel();
		String currentPage = request.getParameter("currentPage");
		int currentPageNo = currentPage != null && !"".equals("currentPage") ? Integer
				.valueOf(currentPage) : 1;
		pageModel.setCurrentPage(currentPageNo);
		// 条件查询
		PaperEntity paper = new PaperEntity();
		String paperName = request.getParameter("paperName");
		if (paperName != null && !"".equals(paperName)) {
			paper.setPaperName(paperName);
		}
		String subName = request.getParameter("subjectName");
		if (subName != null && !"".equals(subName)) {
			String subjectName = "%" + subName + "%";
			String subject_id = PageService.getId(subjectName);
			paper.setSubjectId(subject_id);
		}

		PageModel paging = PageService.maintain(pageModel, paper);

		request.setAttribute("paging", paging);
		request.getRequestDispatcher("view/teacher/paper-admin/paper-scan.jsp")
				.forward(request, response);
	}

	/**
	 * 自动生成试卷
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @throws IOException
	 * @throws ServletException
	 */
	private void autoBuild(HttpServletRequest request,
			HttpServletResponse response, PrintWriter out)
			throws ServletException, IOException {
		IPageService PageService = new pageServiceImpl();
		// 获取试卷列表信息
		String paperId = request.getParameter("paperId");
		Map<String, Object> map = PageService.getPaperById(paperId);

		request.setAttribute("paper", map);

		// 初始化试卷的试题
		List<Map<String, Object>> singleList = PageService
				.getQuestionsByTypeId("1",
						String.valueOf(map.get("single_number")));
		List<Map<String, Object>> multiList = PageService.getQuestionsByTypeId(
				"2", String.valueOf(map.get("multi_number")));
		List<Map<String, Object>> judgeList = PageService.getQuestionsByTypeId(
				"3", String.valueOf(map.get("judge_number")));
		if (judgeList != null && multiList != null && singleList != null) {
			out.print("<script>alert('试卷生成成功');location='PageServlet?command=paperBuild'</script>");
		} else {
			out.print("<script>alert('试卷生成失败')</script>");
		}
	}

	/**
	 * 初始化试卷列表
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @throws IOException
	 * @throws ServletException
	 */
	private void paperBuild(HttpServletRequest request,
			HttpServletResponse response, PrintWriter out)
			throws ServletException, IOException {
		IPageService PageService = new pageServiceImpl();

		// 分页查询
		PageModel pageModel = new PageModel();
		String currentPage = request.getParameter("currentPage");
		int currentPageNo = currentPage != null && !"".equals("currentPage") ? Integer
				.valueOf(currentPage) : 1;
		pageModel.setCurrentPage(currentPageNo);
		// 条件查询
		PaperEntity paper = new PaperEntity();
		String paperName = request.getParameter("paperName");
		if (paperName != null && !"".equals(paperName)) {
			paper.setPaperName(paperName);
		}
		String subName = request.getParameter("subjectName");
		if (subName != null && !"".equals(subName)) {
			String subjectName = "%" + subName + "%";
			String subject_id = PageService.getId(subjectName);
			paper.setSubjectId(subject_id);
		}

		PageModel paging = PageService.maintain(pageModel, paper);
		request.setAttribute("paging", paging);
		request.getRequestDispatcher("view/teacher/paper-admin/paper-build.jsp")
				.forward(request, response);
	}

	/**
	 * 添加试卷
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @throws IOException
	 */
	private void addPaper(HttpServletRequest request,
			HttpServletResponse response, PrintWriter out) throws IOException {
		PaperEntity paper = new PaperEntity();
		String paperName = request.getParameter("paperName");
		String subjectNo = request.getParameter("subjectNo");
		String totalScore = request.getParameter("totalScore");
		String passScore = request.getParameter("passScore");
		String examTime = request.getParameter("examTime");
		String singleNumber = request.getParameter("singleNumber");
		String singleScore = request.getParameter("singleScore");
		String multiNumber = request.getParameter("multiNumber");
		String multiScore = request.getParameter("multiScore");
		String judgeNumber = request.getParameter("judgeNumber");
		String judgeScore = request.getParameter("judgeScore");
		String userId = request.getParameter("userId");

		paper.setExamTime(examTime);
		paper.setJudgeNumber(judgeNumber);
		paper.setJudgeScore(judgeScore);
		paper.setMultiNumber(multiNumber);
		paper.setMultiScore(multiScore);
		paper.setPaperName(paperName);
		paper.setPassScore(passScore);
		paper.setSingleNumber(singleNumber);
		paper.setSingleScore(singleScore);
		paper.setSubjectId(subjectNo);
		paper.setTotalScore(totalScore);
		paper.setUserId(userId);

		IPageService PageService = new pageServiceImpl();
		int row = PageService.addPaper(paper);
		if (row > 0) {
			response.sendRedirect("PageServlet?command=maintain");
		} else {
			out.print("<script>alert('添加失败')</script>");
		}
	}

	/**
	 * 初始化科目名称
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @throws IOException
	 * @throws ServletException
	 */
	private void initType(HttpServletRequest request,
			HttpServletResponse response, PrintWriter out)
			throws ServletException, IOException {
		IPageService PageService = new pageServiceImpl();
		List<Map<String, Object>> subjectNameList = PageService.initType();
		List<Map<String, Object>> userNameList = PageService.initUserName();

		if (subjectNameList != null && userNameList != null) {
			request.setAttribute("subjectName", subjectNameList);
			request.setAttribute("userName", userNameList);
			request.getRequestDispatcher(
					"view/teacher/paper-admin/paper-addpaper.jsp").forward(
					request, response);
		} else {
			out.print("<script>alert('初始化失败')</script>");
		}

	}

	/**
	 * 修改试卷信息
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @throws IOException
	 */
	private void update(HttpServletRequest request,
			HttpServletResponse response, PrintWriter out) throws IOException {

		PaperEntity paper = new PaperEntity();
		String paperName = request.getParameter("paperName");
		String subjectNo = request.getParameter("subjectNo");
		String totalScore = request.getParameter("totalScore");
		String passScore = request.getParameter("passScore");
		String examTime = request.getParameter("examTime");
		String singleNumber = request.getParameter("singleNumber");
		String singleScore = request.getParameter("singleScore");
		String multiNumber = request.getParameter("multiNumber");
		String multiScore = request.getParameter("multiScore");
		String judgeNumber = request.getParameter("judgeNumber");
		String judgeScore = request.getParameter("judgeScore");
		String userId = request.getParameter("userId");
		String paperId = request.getParameter("paperId");

		paper.setExamTime(examTime);
		paper.setJudgeNumber(judgeNumber);
		paper.setJudgeScore(judgeScore);
		paper.setMultiNumber(multiNumber);
		paper.setMultiScore(multiScore);
		paper.setPaperId(paperId);
		paper.setPaperName(paperName);
		paper.setPassScore(passScore);
		paper.setSingleNumber(singleNumber);
		paper.setSingleScore(singleScore);
		paper.setSubjectId(subjectNo);
		paper.setTotalScore(totalScore);
		paper.setUserId(userId);

		IPageService PageService = new pageServiceImpl();
		int row = PageService.update(paper);
		if (row > 0) {
			response.sendRedirect("PageServlet?command=maintain");
		} else {
			out.print("<script>alert('修改失败')</script>");
		}
	}

	/**
	 * 初始化修改操作
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @throws IOException
	 * @throws ServletException
	 */
	private void initUpdate(HttpServletRequest request,
			HttpServletResponse response, PrintWriter out)
			throws ServletException, IOException {
		String paperId = request.getParameter("paperId");
		IPageService PageService = new pageServiceImpl();
		Map<String, Object> paper = PageService.initUpdate(paperId);
		if (paper != null) {
			request.setAttribute("paper", paper);
			request.getRequestDispatcher(
					"view/teacher/paper-admin/paper-updatepaper.jsp").forward(
					request, response);
		} else {
			out.print("<script>alter('初始化失败')</script>");
		}
	}

	/**
	 * 刪除試卷
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @throws IOException
	 */
	private void deletePaper(HttpServletRequest request,
			HttpServletResponse response, PrintWriter out) throws IOException {
		IPageService PageService = new pageServiceImpl();
		String paperId = request.getParameter("paperId");

		int row = PageService.deletePaper(paperId);
		if (row > 0) {
			response.sendRedirect("PageServlet?command=maintain");
		} else {
			out.print("<script>alert('删除失败')</script>");
		}
	}

	/**
	 * 试卷列表
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @throws IOException
	 * @throws ServletException
	 */
	private void maintain(HttpServletRequest request,
			HttpServletResponse response, PrintWriter out)
			throws ServletException, IOException {
		IPageService PageService = new pageServiceImpl();

		// 分页查询
		PageModel pageModel = new PageModel();
		String currentPage = request.getParameter("currentPage");
		int currentPageNo = currentPage != null && !"".equals("currentPage") ? Integer
				.valueOf(currentPage) : 1;
		pageModel.setCurrentPage(currentPageNo);
		// 条件查询
		PaperEntity paper = new PaperEntity();
		String paperName = request.getParameter("paperName");
		if (paperName != null && !"".equals(paperName)) {
			paper.setPaperName(paperName);
		}
		String subName = request.getParameter("subjectName");
		if (subName != null && !"".equals(subName)) {
			String subjectName = "%" + subName + "%";
			String subject_id = PageService.getId(subjectName);
			paper.setSubjectId(subject_id);
		}

		PageModel paging = PageService.maintain(pageModel, paper);
		request.setAttribute("paging", paging);
		request.getRequestDispatcher(
				"view/teacher/paper-admin/paper-maintain.jsp").forward(request,
				response);
	}

}
